package cn.lcvc.xnc.authority.service.impl;

import cn.lcvc.xnc.authority.dao.BaseMapper;
import cn.lcvc.xnc.authority.service.BaseService;
import cn.lcvc.xnc.application.common.exception.model.MyWebException;
import cn.lcvc.xnc.application.common.exception.model.base.MyRollBackException;
import cn.lcvc.xnc.application.common.utils.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 公共业务的实现类
 * @param <T> 模块的实体类
 * @param <Q> 模块的实体查询类
 */
@Service
@Validated
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
public abstract class IBaseService<T,Q> implements BaseService<T,Q> {
    @Autowired
    protected BaseMapper<T> mapper;
//    private IBaseService iBaseService = MySpringBeanUtil.getBean(IBaseService.class);

    @Override
    public boolean isSyperUser(long userId){
        boolean result=false;
        if(userId==-99||userId==-9){
            result=true;
        }
        return result;
    }

    @Override
    public T get(@NotNull(message="传递参数不能为空") Serializable id) throws MyWebException {
        T result=null;
        //适合读取数据前的验证
        beforeGet(id);
        //从数据库读取记录
        result=mapper.findById(id);
        //读取后的数据处理，例如字段修改，添加关联属性
        afterGet(result);
        return result;

    }

    @Override
    public void save(@NotNull(message="传递参数不能为null") @Valid T t) {
        //适合保存数据前的验证
        beforeSave(t);
        //保存到数据库。执行后，添加后的主键会赋值在t上
        mapper.save(t);
        //保存数据后的处理
        afterSave(t);
    }

    @Override
    public void update(@NotNull(message="传递参数不能为null") @Valid T t) {
        //适合更新数据前的验证
        beforeUpdate(t);
        mapper.update(t);
        //更新数据后的处理
        afterUpdate(t);
    }


    @Override
    public void delete(@NotEmpty(message = "操作失败：请选择要移除的记录") Serializable[] ids) {
        beforeDelete(ids);
        mapper.deleteByIdIn(ids);
        afterDelete(ids);
    }

    @Override
    public List<T> findAll(Q query) {
        List<T> list=new ArrayList<>();
        beforeFindAll(query);
        list=mapper.findAll(query);
        afterFindAll(list);
        return list;
    }

    @Override
    public Pagination findAll(Integer page, Integer pageSize, Q query) {
        beforeFindAll(query);
        //验证并计算相关分页数据
        Pagination pagination = new Pagination(page, pageSize,mapper.count(query));
        //为当前页的数据集赋值
        pagination.setData(mapper.find(pagination.getOffset(),pagination.getPageSize(),query));
        //对账户信息进行相应处理
/*        pagination.getData().stream().forEach(user->{

        });*/
        afterFindAll(pagination.getData());
        return pagination;
    }

    //在get函数之前执行
    protected abstract void beforeGet(Serializable id);

    //在get函数之后执行
    protected void afterGet(T t){

    }

    //在update函数之前执行
    protected abstract void beforeUpdate(T t);

    protected void afterUpdate(T t){}

    //在添加函数之前执行
    protected abstract void beforeSave(T t);

    //在添加函数之后执行
    protected void afterSave(T t){}

    protected abstract void beforeDelete(Serializable[] ids);

    protected void afterDelete(Serializable[] ids){}

    /*在findall函数之前执行*/
    protected void beforeFindAll(Q query){}

    /*在findall函数之后执行*/
    protected void afterFindAll(List<T> list){}



}
